﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using QLTT.Models;
using QLTT.Commands;
using System.Windows.Input;

namespace QLTT.ViewModels
{
    class AllRoomViewModel:WorkspaceViewModel
    {
        private ObservableCollection<RoomViewModel> _lstRoomViewModel;
        private icanDataContext _dataContext;
        private DelegateCommand _findCommand;
        private DelegateCommand _addCommand;
        private DelegateCommand _deleteCommand;
        private DelegateCommand _showClassCommand;
        private IQueryable<RoomViewModel> _query;
        private string _roomName;
        public AllRoomViewModel(icanDataContext dataContext)
        {
            _lstRoomViewModel = new ObservableCollection<RoomViewModel>();
            _dataContext = dataContext;
            base.DisplayName = Properties.Resources.Base_DisplayName_AllRoom;
        }
        public AllRoomViewModel(icanDataContext dataContext, bool isGetAll)
        {
            _lstRoomViewModel = new ObservableCollection<RoomViewModel>();
            _dataContext = dataContext;
            if (isGetAll)
            {
                _query = from room in _dataContext.Rooms orderby room.ID descending select new RoomViewModel(_dataContext, room);
                this.NumRow = _query.Count();
                this.CurPage = 1;
                this.UpdateList();
            }
            base.DisplayName = Properties.Resources.Base_DisplayName_AllRoom;
        }
        public string Name
        {
            get { return _roomName; }
            set { _roomName = value; }
        }
        
        public ObservableCollection<RoomViewModel> AllRooms
        {
            get { return _lstRoomViewModel; }
            set {
                _lstRoomViewModel = value;
                base.OnPropertyChanged("AllRooms");
            }
        }
        public ICommand FindCommand
        {
            get
            {
                if (_findCommand == null)
                {
                    _findCommand = new DelegateCommand(Find);
                }
                return _findCommand;
            }
        }
        public ICommand AddCommand
        {
            get
            {
                if (_addCommand == null)
                {
                    _addCommand = new DelegateCommand(AddRoom);
                }
                return _addCommand;
            }
        }
        public ICommand DeleteCommand
        {
            get
            {
                if (_deleteCommand == null)
                {
                    _deleteCommand = new DelegateCommand(DeleteRoom, CanDeleteRoom);
                }
                return _deleteCommand;
            }
        }
        public ICommand ShowClassCommand
        {
            get
            {
                if (_showClassCommand == null)
                {
                    _showClassCommand = new DelegateCommand(ShowClass, CanShowClass);
                }
                return _showClassCommand;
            }
        }
        public void Find()
        {
            IQueryable<Room> myQuery = _dataContext.Rooms;
            if (!string.IsNullOrEmpty(_roomName))
                myQuery = myQuery.Where<Room>(room => room.Name.Contains(_roomName));
            //Add more Where Conditions here
            //Order
            myQuery = myQuery.OrderByDescending(r => r.ID);
            //Select
            _query = myQuery.Select(room => new RoomViewModel(_dataContext, room));
            this.NumRow = _query.Count();
            //Reset to page 0
            this.CurPage = 1;
            this.UpdateList();
        }
        
        public void AddRoom()
        {
            RoomViewModel ws = new RoomViewModel(_dataContext);
            Mediator.NotifyColleagues<WorkspaceViewModel>(Messages.WorkspaceAdd, ws);
        }
        
        private void DeleteRoom()
        {
            foreach (RoomViewModel tvm in AllRooms)
            {
                if (tvm.IsSelected)
                {
                    tvm.Delete();
                }
            }
            this.NumRow = _query.Count();
            this.UpdateList();

        }
        private bool CanDeleteRoom()
        {
            if (ViewModelBase.CurrentRole != UserRole.Administrator)
                return false; 
            RoomViewModel rVM = AllRooms.FirstOrDefault(r => r.IsSelected == true);
            if (rVM == null)
                return false;
            return rVM.CanDelete;
        }
        public void ShowClass()
        {
            RoomViewModel tVM = this.AllRooms.FirstOrDefault(t => t.IsSelected == true);
            if (tVM == null)
                return;
            ClassScheduleViewModel cVM = new ClassScheduleViewModel(_dataContext, false);
            cVM.Room = tVM.ID;
            cVM.Find();
            Mediator.NotifyColleagues<WorkspaceViewModel>(Messages.WorkspaceAdd, cVM);
        }
        public bool CanShowClass()
        {
            return this.AllRooms.Where(t => t.IsSelected == true).Count() > 0;
        }
        [MediatorLib.MediatorMessageSink(Messages.ListRoomUpdated, ParameterType = typeof(Room))]
        public void UpdateListRoom(Room room)
        {
            this.NumRow = _query.Count();
            this.UpdateList();
        }

        public override void UpdateList()
        {
            List<RoomViewModel> lstRooms = _query.Skip((this.CurPage - 1) * this.RowsPerPage).Take(this.RowsPerPage).ToList();
            AllRooms = new ObservableCollection<RoomViewModel>(lstRooms);
            base.UpdateList();
        }
    }
}
